package com.gitee.wsl.android.ext.device

import android.content.pm.PackageManager
import android.graphics.SurfaceTexture

import android.hardware.Camera
import java.io.IOException
import android.hardware.Camera.Parameters.FLASH_MODE_OFF
import android.hardware.Camera.Parameters.FLASH_MODE_TORCH
import com.gitee.wsl.android.ext.ApplicationInit.application
import com.gitee.wsl.android.ext.device.FlashlightExt.destroy
import com.gitee.wsl.android.ext.device.FlashlightExt.setFlashlightStatus
import timber.log.Timber

/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2018/04/27
 *     desc  : utils about flashlight
 * </pre>
 */

fun sampleFlashlight(isOn: Boolean=true,callHandler: ()->Unit){
    try {
        setFlashlightStatus(isOn)
        callHandler()
        if(isOn) setFlashlightStatus(false)
    } finally {
        destroy()
    }
}

object FlashlightExt {
    private var mCamera: Camera? = null
    private var mSurfaceTexture: SurfaceTexture? = null


    /**
     * Return whether the device supports flashlight.
     *
     * @return `true`: yes<br></br>`false`: no
     */
    fun isFlashlightEnable(): Boolean {
        return application.packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)
    }

    /**
     * Return whether the flashlight is working.
     *
     * @return `true`: yes<br></br>`false`: no
     */
    fun isFlashlightOn(): Boolean {
        if (!init()) return false
        val parameters: Camera.Parameters = mCamera!!.getParameters()
        return FLASH_MODE_TORCH == parameters.getFlashMode()
    }

    /**
     * Turn on or turn off the flashlight.
     *
     * @param isOn True to turn on the flashlight, false otherwise.
     */
    fun setFlashlightStatus(isOn: Boolean) {
        if (!init()) return
        val parameters: Camera.Parameters = mCamera!!.getParameters()
        if (isOn) {
            if (!FLASH_MODE_TORCH.equals(parameters.getFlashMode())) {
                try {
                    mCamera!!.setPreviewTexture(mSurfaceTexture)
                    mCamera!!.startPreview()
                    parameters.setFlashMode(FLASH_MODE_TORCH)
                    mCamera!!.setParameters(parameters)
                } catch (e: IOException) {
                    e.printStackTrace()
                }
            }
        } else {
            if (!FLASH_MODE_OFF.equals(parameters.getFlashMode())) {
                parameters.setFlashMode(FLASH_MODE_OFF)
                mCamera!!.setParameters(parameters)
            }
        }
    }

    /**
     * Destroy the flashlight.
     */
    fun destroy() {
        if (mCamera == null) return
        mCamera?.release()
        mSurfaceTexture = null
        mCamera = null
    }

    private fun init(): Boolean {
        if (mCamera == null) {
            try {
                mCamera = Camera.open(0)
                mSurfaceTexture = SurfaceTexture(0)
            } catch (t: Throwable) {
                Timber.d( "init failed: ", t)
                return false
            }
        }
        if (mCamera == null) {
            Timber.d("init failed.")
            return false
        }
        return true
    }
}